package net.goutalk.glcs.module.erpModel.caseErpStore.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.goutalk.glcs.common.annotation.GlcsLog;
import net.goutalk.glcs.common.constant.GlobalConstant;
import net.goutalk.glcs.common.model.result.R;
import net.goutalk.glcs.common.page.ConventPage;
import net.goutalk.glcs.common.page.PageOutput;
import net.goutalk.glcs.common.utils.VoToColumnUtil;
import net.goutalk.glcs.module.erpModel.caseErpStore.dto.AddCaseErpStoreReceiptDto;
import net.goutalk.glcs.module.erpModel.caseErpStore.dto.CaseErpStoreReceiptPageDto;
import net.goutalk.glcs.module.erpModel.caseErpStore.dto.UpdateCaseErpStoreReceiptDto;
import net.goutalk.glcs.module.erpModel.caseErpStore.entity.CaseErpStoreReceipt;
import net.goutalk.glcs.module.erpModel.caseErpStore.entity.CaseErpStoreReceiptDetail;
import net.goutalk.glcs.module.erpModel.caseErpStore.service.ICaseErpStoreReceiptDetailService;
import net.goutalk.glcs.module.erpModel.caseErpStore.service.ICaseErpStoreReceiptService;
import net.goutalk.glcs.module.erpModel.caseErpStore.vo.CaseErpStoreReceiptInfoVo;
import net.goutalk.glcs.module.erpModel.caseErpStore.vo.CaseErpStoreReceiptPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 出入库单据信息表【case_erp_store_receipt】 前端控制器
 * </p>
 *
 * @author hnyyzy
 * @since 2023-07-20
 */
@RestController
@RequestMapping(GlobalConstant.CASE_ERP_STOR_RECCCEIPT + "/caseErpStoreReceipt")
@Api(value = GlobalConstant.CASE_ERP_STOR_RECCCEIPT + "/caseErpStoreReceipt", tags = "出入库单据信息")
@AllArgsConstructor
public class CaseErpStoreReceiptController {

    private ICaseErpStoreReceiptService caseErpStoreReceiptService;

    private ICaseErpStoreReceiptDetailService caseErpStoreReceiptDetailService;

    @GetMapping(value = "/page")
    @ApiOperation("出入库单据信息列表分页")
    public R page(CaseErpStoreReceiptPageDto dto) {
        LambdaQueryWrapper<CaseErpStoreReceipt> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(StrUtil.isNotBlank(dto.getKeyword()), CaseErpStoreReceipt::getCode,dto.getKeyword())
                .eq(ObjectUtil.isNotEmpty(dto.getType()),CaseErpStoreReceipt::getStoreType,dto.getType())
                .or()
                .like(StrUtil.isNotBlank(dto.getKeyword()), CaseErpStoreReceipt::getName,dto.getKeyword())
                .eq(ObjectUtil.isNotEmpty(dto.getType()),CaseErpStoreReceipt::getStoreType,dto.getType())
                .orderByDesc(CaseErpStoreReceipt::getCreateDate)
                .select(CaseErpStoreReceipt.class, x -> VoToColumnUtil.fieldsToColumns(CaseErpStoreReceiptPageVo.class).contains(x.getProperty()));
        IPage<CaseErpStoreReceipt> page = caseErpStoreReceiptService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<CaseErpStoreReceiptPageVo> pageOutput = ConventPage.getPageOutput(page, CaseErpStoreReceiptPageVo.class);
        return R.ok(pageOutput);
    }

    @PostMapping
    @ApiOperation(value = "新增出入库单据信息")
    @Transactional
    public R add(@RequestBody AddCaseErpStoreReceiptDto dto) {
        return R.ok(caseErpStoreReceiptService.add(dto));
    }


    @PutMapping
    @ApiOperation(value = "修改出入库单据信息")
    @Transactional
    public R update(@Valid @RequestBody UpdateCaseErpStoreReceiptDto dto) {
        return R.ok(caseErpStoreReceiptService.edit(dto));
    }


    @GetMapping(value = "/info")
    @ApiOperation(value = "根据id查询出入库单据信息")
    public R info(@RequestParam Long id) {
        CaseErpStoreReceipt caseErpStoreReceipt = caseErpStoreReceiptService.getById(id);
        if (caseErpStoreReceipt == null) {
            R.error("找不到此出入库单据信息");
        }
        CaseErpStoreReceiptInfoVo caseErpStoreReceiptInfoVo = BeanUtil.toBean(caseErpStoreReceipt, CaseErpStoreReceiptInfoVo.class);
        List<CaseErpStoreReceiptDetail> list = caseErpStoreReceiptDetailService.list(Wrappers.<CaseErpStoreReceiptDetail>query().lambda().eq(CaseErpStoreReceiptDetail::getStoreReceiptId, caseErpStoreReceipt.getId()));
        caseErpStoreReceiptInfoVo.setCaseErpStoreReceiptDetailList(list);
        return R.ok(caseErpStoreReceiptInfoVo);
    }


    @DeleteMapping
    @ApiOperation(value = "删除")
    @GlcsLog(value = "删除出入库单据信息")
    public R delete(@Valid @RequestBody Long id) {
        return R.ok(caseErpStoreReceiptService.delete(id));
    }
}
